package com.ccx.coachsite.ui.customview;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import com.ccx.coachsite.R;
import com.ccx.coachsite.data.SystemConst;

/**
 * 支持横向滑屏ViewGroup <br>
 * Author:ryan <br>
 * Date:2011-7-29下午05:33:47
 */
public class ViewPagerTab extends View implements IPageBar {
	private static final String SELECTCOLOR = "#ffffff";
	private int selectColor = Color.BLUE;
	private float maxWidth = 0, distanceScale, touchedTab = -1;
	private int hlLineLeft, lineTop, textMargin, textHeight,
			textTop, hlLineWidth, hlLineInitCenter;

	public int selectTab = SystemConst.NO_DATA;
	
	private ViewPager viewpager;
	private Drawable line, hlLine;
	private Paint textPaint = new Paint();
	private List<TabInfo> tabs = new ArrayList<TabInfo>();

	PopupWindow popup;

	public ViewPagerTab(Context context) {
		super(context);
		init(context);
	}

	public ViewPagerTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public ViewPagerTab(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {

		setBackgroundResource(R.drawable.flow_bar_tab_bg);

		textPaint.setAntiAlias(true);
		textPaint.setShadowLayer(1, 1, 1, Color.BLACK);
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(context.getResources().getDimensionPixelSize(
				R.dimen.viewpager_tab_textsize));
		textHeight = textPaint.getFontMetricsInt(null);
		textMargin = textHeight / 2;

		selectColor = Color.parseColor(SELECTCOLOR);
		line = context.getResources().getDrawable(
				R.drawable.flow_bar_bottom_line);
		hlLine = context.getResources().getDrawable(R.drawable.flow_bar_hl);
	}

	public void addTitle(String title[]) {
		final int N = title.length;
		for (int i = 0; i < N; i++) {
			TabInfo tab = new TabInfo();
			tab.title = title[i];
			tab.width = textPaint.measureText(tab.title);
			tabs.add(tab);
			if (tab.width > maxWidth)
				maxWidth = tab.width;
		}
		if (selectTab == SystemConst.NO_DATA)
			selectTab = (N - 1)/ 2;
		
		hlLineWidth = (int) (maxWidth * 3 / 2);
	}

	public void setTextSize(int size) {
		textPaint.setTextSize(size);
	}

	class TabInfo {
		String title;
		float width;
		float left, top, center;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int N = tabs.size();
		// if (N <= 1)
		// return;

		final int width = MeasureSpec.getSize(widthMeasureSpec);
		final int height = MeasureSpec.getSize(heightMeasureSpec);
		textTop = (height - (textHeight * 2 + textMargin)) / 2 + textHeight
				+ textMargin;
		lineTop = textTop + textMargin;
		float margin = (width - N * maxWidth) / N;
		float startLeft = margin / 2 + (maxWidth - tabs.get(0).width) / 2;
		for (int i = 0; i < N; i++) {
			TabInfo info = tabs.get(i);
			info.left = startLeft + (maxWidth + margin) * i
					+ (maxWidth - info.width) / 2;
			info.center = info.left + info.width / 2;
			info.top = textTop;
		}
		hlLineLeft = (int) (tabs.get(0).center - hlLineWidth / 2);
		if (N > 1)
			distanceScale = (tabs.get(1).center - tabs.get(0).center) / width;
		else
			distanceScale = .5f;
		line.setBounds(0, lineTop, width, lineTop + textMargin / 2);
		hlLineInitCenter = (int) (tabs.get(selectTab).center - hlLineWidth / 2);
		hlLine.setBounds(hlLineInitCenter, lineTop, hlLineInitCenter
				+ hlLineWidth, lineTop + textMargin / 2);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		final int N = tabs.size();
		// if (N <= 1)
		// return;

		for (int i = 0; i < N; i++) {
			TabInfo info = tabs.get(i);
			if (i == selectTab || i == touchedTab)
				textPaint.setColor(selectColor);
			else
				textPaint.setColor(0xFFAAAAAA);

			canvas.drawText(info.title, info.left, info.top, textPaint);
		}

		line.draw(canvas);
		hlLine.draw(canvas);
	}

	@Override
	public void scrollHighLight(int scrollX) {
		hlLineInitCenter = hlLineLeft + (int) (scrollX * distanceScale);
		hlLine.setBounds(hlLineInitCenter, lineTop, hlLineInitCenter
				+ hlLineWidth, lineTop + textMargin / 2);
		invalidate();
	}

	@Override
	public void updateSelected(int page) {
		Rect bound = hlLine.getBounds();
		final int y = bound.top + (bound.bottom - bound.top) / 2;
		for (int i = 0; i < tabs.size(); i++) {
			if (!bound.contains((int) tabs.get(i).center, y))
				continue;

			selectTab = i;
			invalidate();
			break;
		}
	}

	public int getSelectedTab() {
		return selectTab;
	}

	public void setViewpager(ViewPager viewpager) {
		this.viewpager = viewpager;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		if (action == MotionEvent.ACTION_UP
				|| action == MotionEvent.ACTION_CANCEL) {
			TabInfo hintInfo = getHitRangeInfo(event);
			if (hintInfo != null) {
				/*
				 * if ( getSelectedTab()==tabs.indexOf(hintInfo) ) {
				 * 
				 * int tmpInt= (int) (hintInfo.center - hlLineWidth / 2);
				 * 
				 * View layout; layout = View.inflate(getContext(),
				 * R.layout.popup_shortcutmenu, null); popup = new
				 * PopupWindow(layout,hlLineWidth,
				 * LayoutParams.WRAP_CONTENT,true);
				 * popup.setBackgroundDrawable(new BitmapDrawable());
				 * 
				 * popup.showAsDropDown(this, tmpInt, -textMargin ); }
				 */
				viewpager.snapToScreen(tabs.indexOf(hintInfo));
			}
			touchedTab = -1;
			invalidate();
		} else if (action == MotionEvent.ACTION_DOWN) {
			TabInfo hintInfo = getHitRangeInfo(event);
			if (hintInfo != null) {
				touchedTab = tabs.indexOf(hintInfo);
				invalidate();
			}
		}
		return true;
	}

	private TabInfo getHitRangeInfo(MotionEvent e) {
		int x = Math.round(e.getX());
		int y = Math.round(e.getY());
		Rect hitRect;
		for (int index = 0; index < tabs.size(); index++) {
			TabInfo info = tabs.get(index);
			hitRect = new Rect((int) (info.center - info.width),
					(int) (info.top - textHeight - textMargin),
					(int) (info.center + info.width),
					(int) (info.top + textMargin));
			if (hitRect.contains(x, y)) {
				return info;
			}
		}

		return null;
	}
	
	/**
	 * 指定默认显示的TAB
	 */
	public void setInitTab(int tab) {
		this.selectTab = tab;
	}
}
